package com.camps.sdk.decrypt;

import com.camps.sdk.common.Constants;
import com.camps.sdk.entity.DeviceUpMessage;
import com.camps.sdk.entity.LocationData;
import com.camps.sdk.entity.SOSList;
import com.camps.sdk.entity.SOSNumber;
import com.camps.sdk.utils.LocationUtils;
import com.camps.sdk.utils.UnicodeUtil;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author ldaoliang
 * @Date create in 15:58 2021/9/1
 * @Description
 */
public class AnalyzeSetSos implements AnalyticalData {
    @Override
    public DeviceUpMessage parseData(String originalData) {
        //解析到指令：*WT,868976030203014,V4,SETSOS,162040,162010,1,\u6d4b\u0031,18797874506,2,\u6d4b\u0032,13669061320,3,\u6d4b\u0032,13669061323,V,0.0000,S,0.0000,W,0.00,0,210721,FDFFFFFF#
        //解密
        String[] split = parseCommand(originalData);
        int length = split.length;
        String imei = split[1];//imei号
        String commandNumber = split[4];//流水号
        List<SOSNumber> deviceSOSLists = new ArrayList<>();
        //固定数据一定是3组(解析参数)
        List<Integer> sequenceList = new ArrayList<>();
        for (int j = 1; j <= 3; j++) {
            //假如是第一组 split[6]
            sequenceList.add(Integer.parseInt(split[5+(j-1)*3+1]));
            SOSNumber deviceSOSList = new SOSNumber();
            //这里只存发送的参数，name,phone,sequence
            deviceSOSList.setSequence(Integer.parseInt(split[5 + (j - 1) * 3 + 1]));
            String name = split[5 + (j - 1) * 3 + 2];
            if(name!=null && name.equals("")){
                name = UnicodeUtil.decodeUnicode(name);
            }
            deviceSOSList.setSosName(name);
            deviceSOSList.setSosPhone(split[5 + (j - 1) * 3 + 3]);
            deviceSOSLists.add(deviceSOSList);
        }
        String time = split[5];
        //年月日
        String date = split[length - 2];
        String orientation = split[length - 3];
        String speed = split[length - 4];
        String lng = split[length - 6];
        String lat = split[length - 8];
        //设备状态
        String deviceStatus = split[length - 1];
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        SOSList sosList = new SOSList();
        sosList.setCommandNumber(commandNumber);
        sosList.setDeviceDate(date);
        sosList.setDeviceTime(time);
        sosList.setImei(imei);
        sosList.setDeviceStatus(deviceStatus);
        LocationData LocationData = new LocationData();
        LocationData.setOrientation(Integer.parseInt(orientation));
        LocationData.setSpeed(Double.parseDouble(speed));
        LocationData.setLatitude(list.get(1));
        LocationData.setLongitude(list.get(0));
        sosList.setLocationData(LocationData);
        sosList.setList(deviceSOSLists);
        return structureMessage(Constants.REPLY,Constants.CMD_SET_SOS_PHONE,false,"",sosList);
    }
}
